import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:mall/constant/app_colors.dart';
import 'package:mall/constant/app_dimens.dart';
import 'package:mall/constant/app_strings.dart';
import 'package:mall/constant/text_style.dart';
import 'package:mall/modules/model/foot_print_entity.dart';
import 'package:mall/modules/sc/foot_print_sc.dart';
import 'package:mall/modules/service/mine_service.dart';
import 'package:mall/ui/widgets/cached_image.dart';
import 'package:mall/utils/hud/hud_util.dart';
import 'package:mall/utils/list_placeholder_component/list_placeholder_component.dart';
import 'package:mall/router/navigator_util.dart';
import 'package:mall/utils/refresh/refresh_widget.dart';
import 'package:mall/utils/refresh/warp_refresh_controller.dart';

import 'package:flutter_screenutil/flutter_screenutil.dart';

class FootPrintPage extends StatefulWidget {
  @override
  _FootPrintPageState createState() => _FootPrintPageState();
}

class _FootPrintPageState extends State<FootPrintPage> {
  // var _pageIndex = 1;
  // var _pageSize = 10;
  // FootPrintViewModel _footPrintViewModel = FootPrintViewModel();
  // RefreshController _refreshController = RefreshController();

  final sc_foot_print = Get.put(FootPrintListSc());
  late final _refreshController = WarpRefreshController(loadState: sc_foot_print.loadState);
  RxBool isShowCheckBox = false.obs;
  // @override
  // void initState() {
  //   super.initState();
  //   _footPrintViewModel.queryFootPrint(_pageIndex, _pageSize);
  // }
  @override
  void dispose() {
    super.dispose();
    _refreshController.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: Text(AppStrings.MINE_FOOTPRINT), centerTitle: true, actions: [
          Container(
            alignment: Alignment.center,
            margin: EdgeInsets.only(right: 15),
            child: InkWell(
              onTap: () => isShowCheckBox.value = !isShowCheckBox.value,
              child: Text(
                isShowCheckBox.value ? AppStrings.CANCEL : AppStrings.SELECT,
                style: FMTextStyle.color_ffffff_size_13,
              ),
            ),
          )
        ]),
        // body: _initView(model),
        body: _contentView(),
        bottomNavigationBar: Visibility(
          visible: isShowCheckBox.value && sc_foot_print.data.length > 0,
          child: Container(
            padding: EdgeInsets.only(bottom: 15, left: 15, right: 15),
            height: ScreenUtil().setHeight(AppDimens.DIMENS_150),
            child: ElevatedButton(
              style: ButtonStyle(
                backgroundColor: WidgetStateProperty.all(AppColors.C_FF5722),
                shape: WidgetStateProperty.all(
                  RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(20),
                  ),
                ),
                elevation: WidgetStateProperty.all(0),
                textStyle: WidgetStateProperty.all(FMTextStyle.color_ffffff_size_15),
              ),
              onPressed: () => _deleteFootPrint(),
              child: Text(AppStrings.DELETE),
            ),
          ),
        ));
  }

  // Widget _initView(FootPrintViewModel footPrintViewModel) {
  //   _pageIndex = footPrintViewModel.canLoadMore ? ++_pageIndex : _pageIndex;
  //   RefreshStateUtil.getInstance().stopRefreshOrLoadMore(_refreshController);
  //   if (footPrintViewModel.pageState == PageState.hasData) {
  //     return _contentView(footPrintViewModel);
  //   }
  //   return ViewModelStateWidget.stateWidgetWithCallBack(footPrintViewModel, _onRefresh);
  // }

  Widget _contentView() {
    return Container(
      margin: EdgeInsets.only(left: 15, right: 15),
      // child: SmartRefresher(
      //   enablePullDown: true,
      //   enablePullUp: footPrintViewModel.canLoadMore,
      //   header: WaterDropMaterialHeader(
      //     backgroundColor: AppColors.COLOR_FF5722,
      //   ),
      //   controller: _refreshController,
      //   onRefresh: () => _onRefresh(),
      //   onLoading: () => _onLoadMore(),
      //   child: GridView.builder(
      //       itemCount: footPrintViewModel.goods.length,
      //       gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
      //         crossAxisCount: 2,
      //         childAspectRatio: 0.8,
      //       ),
      //       itemBuilder: (BuildContext context, int index) {
      //         return getGoodsItemView(footPrintViewModel.goods[index], index);
      //       }),
      child: Obx(
        () => RefreshComponent(
          controller: _refreshController,
          onLoading: () => sc_foot_print.loadMoreData(),
          onRefresh: () => sc_foot_print.refreshData(),
          child: WrappedListPlaceholderComponent.widget(
            sc: sc_foot_print,
            builder: (p0) => GridView.builder(
                itemCount: p0.length,
                gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2,
                  childAspectRatio: 0.8,
                ),
                itemBuilder: (BuildContext context, int index) {
                  return getGoodsItemView(p0[index], index);
                }),
          ),
        ),
      ),
    );
  }

  Widget getGoodsItemView(FootPrintGoodsEntity goods, int index) {
    return GestureDetector(
      child: Card(
          child: Stack(
        children: [
          Container(
              alignment: Alignment.center,
              child: Column(
                children: <Widget>[
                  CachedImageView(
                    ScreenUtil().setHeight(AppDimens.DIMENS_400),
                    ScreenUtil().setHeight(AppDimens.DIMENS_400),
                    goods.picUrl,
                  ),
                  Container(
                    margin: EdgeInsets.only(top: 10, left: 10, right: 10),
                    child: Text(goods.name ?? "",
                        maxLines: 1, overflow: TextOverflow.ellipsis, style: FMTextStyle.color_222222_size_13),
                  ),
                  Padding(padding: EdgeInsets.only(top: 10)),
                  Text(
                    "${AppStrings.DOLLAR}${goods.retailPrice}",
                    maxLines: 1,
                    overflow: TextOverflow.ellipsis,
                    style: FMTextStyle.color_ff5722_size_13,
                  ),
                ],
              )),
          Positioned(
            top: 0,
            right: 0,
            child: Offstage(
                offstage: !isShowCheckBox.value,
                child: Checkbox(
                    hoverColor: AppColors.C_999999,
                    value: goods.isCheck,
                    onChanged: (isCheck) {
                      sc_foot_print.setCheck(index, isCheck ?? false);
                    })),
          )
        ],
      )),
      onTap: () => _itemClick(goods.goodsId),
    );
  }

  // _onRefresh() {
  //   _pageIndex = 1;
  //   _footPrintViewModel.queryFootPrint(_pageIndex, _pageSize);
  // }

  // _onLoadMore() {
  //   _footPrintViewModel.queryFootPrint(_pageIndex, _pageSize);
  // }

  _itemClick(int? goodsId) {
    NavigatorUtil.goGoodsDetails(context, goodsId);
  }

  _deleteFootPrint() {
    final ids = sc_foot_print.getSelectedData();

    MineService.deleteFootPrints(ids).then((response) {
      HUDUtil.toast(AppStrings.DELETE_SUCCESS);
      isShowCheckBox.value = false;
    });
  }
}
